package com.twu.biblioteca.db;

import com.twu.biblioteca.model.Book;
import com.twu.biblioteca.model.User;

import java.util.*;

public class BookStore extends Store {
    Map<Book,User> bookUserMap;

    public BookStore() {
        super();
        items.add(new Book("100", "Head First Java", Arrays.asList("Kathy Sierra", "Bert Bates")));
        items.add(new Book("101", "Effective Java", Arrays.asList("Joshua Bloch")));
        items.add(new Book("102", "Java The Complete Reference, 8th Edition", Arrays.asList("Herbert Schildt")));
        bookUserMap = new HashMap<Book, User>();
    }

    void printHeader() {
        System.out.println("Book Id\t\tName\t\t\tAuthor");
    }

    public boolean checkout(String bookId, User currentUser) {
        Book book =(Book) findById(bookId);
        if(book==null || !book.isAvailable())
            return false;
        book.setUnAvailable();
        bookUserMap.put(book,currentUser);
        return true;
    }

    public boolean returnBook(String bookId) {
        Book book =(Book) findById(bookId);
        if(null == book || book.isAvailable())
            return false;
        book.setAvailable();
        bookUserMap.remove(book);
        return  true;
    }

    public User getUserCheckedOutABook(String bookId) {
        Book book =(Book) findById(bookId);
        return bookUserMap.get(book);
    }
}
